<?php

namespace Modules\Chinadmin\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Modules\Chinadmin\Models\AuthCompanyControllerModel;
use Modules\Chinadmin\Models\AuthRoleControllerModel;
use Modules\Chinadmin\Models\ControllerModel;
use Modules\Chinadmin\Models\RoleModel;
use Modules\System\Services\Mds\MDSArray;

class Role extends Base
{

    /**
     * 依赖注入
     */
    public function __construct(Request $request){
        //自行配制
        parent::__construct();

        $this->models->primaryKey = "role_id";

    }

    /**
     * 路由
     * http://l.com/chinadmin/admin/Role/companyController
     */
    public function companyController(Request $request){

        $i = $request->input();
        $data['id'] = $i['id'];
        return view('chinadmin::admin.' . $this->class . '.companyController',$data);

    }

    /**
     * 路由
     * http://l.com/chinadmin/admin/Role/roleController
     */
    public function roleController(Request $request){

        $i = $request->input();
        $data['id'] = $i['id'];
        return view('chinadmin::admin.' . $this->class . '.roleController',$data);

    }

    /**
     * 路由
     * http://l.com/chinadmin/admin/Role/roleControllerData
     */
    public function roleControllerData(Request $request){

        $i = $request->input();

        if(isset($i['type']) && $i['type'] == 'Company'){
            //选取用户的权限
            $AuthRoleController = AuthCompanyControllerModel::where("role_id",$i['id'])->pluck("controller_id");
        }else{
            //选取用户的权限
            $AuthRoleController = AuthRoleControllerModel::where("role_id",$i['id'])->pluck("controller_id");
        }

        if(!empty($AuthRoleController)){
            $AuthRoleController = $AuthRoleController->toArray();
        }
        $Controller = MDSArray::menuTreeArr(ControllerModel::orderBy("sort","desc")->get()->toArray());
        $re = $this->roleControllerJosn($AuthRoleController,$Controller);

        echo json_encode($re);


    }

    protected function roleControllerJosn($AuthRoleController,$Controller){

        $newData['data'] = [];
        $newData['code'] = 0;
        $newData['msg'] = 0;
        $newData['count'] = 10;

        foreach($Controller as $k=>$v){
            $one = [];
            $one['id'] = $v['controller_id'];
            $one['name'] = $v['name'];
            if(in_array($v['controller_id'],$AuthRoleController)){
                $one['LAY_CHECKED'] = true;
            }
            if(!empty($v['child'])){
                foreach($v['child'] as $kc=>$vc){
                    $two = [];
                    $two['id'] = $vc['controller_id'];
                    $two['name'] = $vc['name'];

                    if(in_array($v['controller_id'],$AuthRoleController)){
                        $two['LAY_CHECKED'] = true;
                    }

                    $one['treeList'][] = $two;

                }
            }
            $newData['data'][] = $one;
        }
        return $newData;
    }



}
